package com.ad.platform.controller;

import com.ad.payload.dto.PfAdvertisementDto;
import com.ad.payload.dto.PfAdvertisementRenewDto;
import com.ad.payload.enums.ResultCode;
import com.ad.payload.model.IdDto;
import com.ad.payload.model.PageDto;
import com.ad.payload.model.PageView;
import com.ad.payload.model.ResultBean;
import com.ad.payload.vo.PfAdvertisementVo;
import com.ad.platform.service.IPfAdvertisementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * <p>
 *    广告 前端控制器
 * </p>
 *
 * @author rclia
 * @since 2021-03-17
 */
@RestController
@RequestMapping("/pfAdvertisement")
public class PfAdvertisementController {

    @Autowired
    private IPfAdvertisementService iPfAdvertisementService;

    /**
     * 新增/更新 广告信息
     * @param pfAdvertisementDto
     * @param bindingResult
     * @return
     */
    @PostMapping("/noAuth/saveOrUpdatePfAdvertisement")
    public ResultBean<String> saveOrUpdatePfAdvertisement(@RequestBody @Valid PfAdvertisementDto pfAdvertisementDto, BindingResult bindingResult){

        for(ObjectError error : bindingResult.getAllErrors()){
            return ResultBean.build(ResultCode.VALIDATE_FAILED.getCode(),error.getDefaultMessage());
        }

        return iPfAdvertisementService.saveOrUpdate(pfAdvertisementDto) ? ResultBean.success("成功") : ResultBean.success("失败");
    }

    /**
     * 后台web端广告分页
     * @param pageDto
     * @return
     */
    @PostMapping("/noAuth/listPagePfAdvertisement")
    public PageView<PfAdvertisementVo> listPagePfAdvertisement(@RequestBody PageDto pageDto){
        PageView<PfAdvertisementVo> pfCategoryVoPageView =
                iPfAdvertisementService.selectPage(pageDto.getPageNo(), pageDto.getPageSize());
        return pfCategoryVoPageView;

    }

    /**
     * 删除广告信息
     * @param idDto
     * @param bindingResult
     * @return
     */
    @PostMapping("/noAuth/deletePfAdvertisement")
    public ResultBean<String> deletePfAdvertisement(@RequestBody @Valid IdDto idDto, BindingResult bindingResult){

        for(ObjectError error : bindingResult.getAllErrors()){
            return ResultBean.build(ResultCode.VALIDATE_FAILED.getCode(),error.getDefaultMessage());
        }

        return iPfAdvertisementService.removeById(idDto.getId()) ? ResultBean.success("成功")
                : ResultBean.success("失败");
    }

    @PostMapping("/noAuth/adRenew")
    public ResultBean<String> adRenew(@RequestBody @Valid PfAdvertisementRenewDto pfAdvertisementRenewDto, BindingResult bindingResult){

        for(ObjectError error : bindingResult.getAllErrors()){
            return ResultBean.build(ResultCode.VALIDATE_FAILED.getCode(),error.getDefaultMessage());
        }

        return iPfAdvertisementService.adRenew(pfAdvertisementRenewDto) ? ResultBean.success("成功")
                : ResultBean.success("失败");
    }

}
